package action
import (
	"signaling/src/comerrors"
	"signaling/src/framework"
	"encoding/json"
	"net/http"
	"strconv"
	"fmt"
)
// const (
// 	CMDNO_PUSH =  1
// 	CMDNO_PULL = 2
// 	CMDNO_ANSWER = 3
// 	CMDNO_STOP_PUSH = 4
// 	CMDNO_STOP_PULL = 5
// )
type sendAnswerAction struct {}

func NewSendAnswerAction () *sendAnswerAction {
	return &sendAnswerAction{}
}

type xrtcSendAnswerReq struct {
	Cmdno int `json:"cmdno"`
	Uid uint64 `json:"uid"`
	StreamName string `json:"stream_name"`
	Answer string `json:"answer"`
	Type string `json:"type"`
	Mode string `json:"mode"`
}
type xrtcSendAnswerResp struct {

	ErrNo int `json:"err_no"`
	ErrMsg string `json:"err_msg"`
}


func (* sendAnswerAction) Execute (w http.ResponseWriter, cr *framework.ComRequest){
	r :=cr.R
	//uid
	var strUid string
	if values,ok := r.Form["uid"];ok {

		strUid = values[0]
	}
	uid , err := strconv.ParseUint(strUid,10,64)
	if err != nil ||uid <=0 {
		cerr := comerrors.New(comerrors.ParamErr,"parse uid error:"+ err.Error())
		writeJsonErrorResponse(cerr,w,cr)
		return 
	}

	//streamName
	var streamName string
	if values,ok := r.Form["streamName"]; ok {
		streamName = values[0]
	}
	if "" == streamName {
		cerr := comerrors.New(comerrors.ParamErr,"streamName is null")
		writeJsonErrorResponse(cerr,w,cr)
		return
	}

	//answer
	var answer string
	if values,ok := r.Form["answer"]; ok {
		answer = values[0]
	}
	if "" == answer {
		cerr := comerrors.New(comerrors.ParamErr,"answer is null")
		writeJsonErrorResponse(cerr,w,cr)
		return
	}
	

	//Type
	var strType string
	if values,ok := r.Form["type"]; ok {
		strType = values[0]
	}
	if "" == strType {
		cerr := comerrors.New(comerrors.ParamErr,"strType is null")
		writeJsonErrorResponse(cerr,w,cr)
		return
	}
	 //Mode
	var mode string
	if values,ok := r.Form["mode"]; ok {
		mode = values[0]
	}
	if "" == mode {
		cerr := comerrors.New(comerrors.ParamErr,"mode is null")
		writeJsonErrorResponse(cerr,w,cr)
		return
	}
	//  fmt.Println(uid,streamName,audio,video)
	req := xrtcSendAnswerReq{

		Cmdno:CMDNO_ANSWER,
		Uid:uid,
		StreamName:streamName,
		Answer:answer,
		Type:strType,
		Mode:mode,
	}
	var resp xrtcSendAnswerResp
	err = framework.Call("xrtc",req, &resp,cr.LogId)
	// fmt.Printf("%+v\n",resp)
	//将数据返回给浏览器
	if err != nil {

		cerr := comerrors.New(comerrors.NetworkErr,"backend process error:"+ err.Error())
		writeJsonErrorResponse(cerr,w,cr)
		return
	}
	if resp.ErrNo != 0 {

		
		cerr := comerrors.New(comerrors.NetworkErr,
			fmt.Sprintf("backend process error:"+ err.Error()))
		writeJsonErrorResponse(cerr,w,cr)
		return
	}
	//构造http 响应体

	httpResp := comHttpResp{
		ErrNo : 0,
		ErrMsg: "success",
	}

	b,  _  := json.Marshal(httpResp)
	cr.Logger.AddNotice("resp", string(b))
	w.Write(b)
}